package com.aurora.oa.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aurora.oa.R;
import com.aurora.oa.login.view.ActivityLogin;
import com.aurora.oa.utils.SharedPreferencesUtil;
import com.aurora.oa.utils.StringUtils;

import java.util.ArrayList;

/**
 * Created by ww on 2017/5/6 0006.
 */
public class SplashActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private LinearLayout dot_layout;

    private ArrayList<Integer> list = new ArrayList<>();

    /**
     * 用于设置自动轮播
     */
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg){
            viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
        //    handler.sendEmptyMessageDelayed(0, 4000);
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!StringUtils.isNullString(SharedPreferencesUtil.getString(this,SharedPreferencesUtil.USER_NAME))){
            startActivity(new Intent(SplashActivity.this,ActivityLogin.class));
            finish();
            return;
        }
        setContentView(R.layout.activity_splash);
        initView();
        initData();
        initListener();

    }
    //初始化视图
    private void initView() {
       
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        dot_layout = (LinearLayout)findViewById(R.id.dot_layout);
    }
    //初始化数据
    private void initData(){
        list.add(R.mipmap.splash_1);
        list.add(R.mipmap.splash_2);

        initDots();
        viewPager.setAdapter(new MyPagerAdapter());
        int centerValue = Integer.MAX_VALUE/2;
        int value = centerValue % list.size();
        //设置viewPager的第一页为最大整数的中间数，实现伪无限循环
        viewPager.setCurrentItem(centerValue-value);
        updateIntroAndDot();
        handler.sendEmptyMessageDelayed(0,4000);
    }
    //初始化文字下方的圆点
    private void initDots() {
        for (int i=0; i< list.size(); i++)
        {
            View view = new View(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(8,8);
            if(i!=0) {
                params.leftMargin = 5;
            }
            view.setLayoutParams(params);
            view.setBackgroundResource(R.drawable.select_dot);
            dot_layout.addView(view);
        }
    }
    //初始化监听器，当页面改变时，更新其相应数据
    private void initListener() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                updateIntroAndDot();
                int currentPage = viewPager.getCurrentItem() % list.size();
                //到了第二页,两秒后跳转到登录
                if(currentPage == list.size()-1){
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(new Intent(SplashActivity.this, ActivityLogin.class));
                            finish();
                        }
                    },2000);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }
    //更新数据与圆点
    private void updateIntroAndDot(){
        int currentPage = viewPager.getCurrentItem() % list.size();
        for (int i = 0; i < dot_layout.getChildCount(); i++)
            dot_layout.getChildAt(i).setEnabled(i==currentPage);
    }
    //ViewPager的主体部分
    class MyPagerAdapter extends PagerAdapter {

        /**
         * 返回多少page
         */
        public int getCount() {
            return Integer.MAX_VALUE;
        }
        /**view滑动到一半时是否创建新的view
         * true:表示不去创建，使用缓存；false:去重新创建
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
        /**
         * 类似于BaseAdapter的getView方法
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = View.inflate(SplashActivity.this, R.layout.item_splash, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            Integer ad = list.get(position%list.size());
            imageView.setImageResource(ad);
            container.addView(view);
            return view;
        }
        /**
         * @param position:当前需要销毁第几个page
         * @param object:当前需要销毁的page
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //super.destroyItem(container, position, object);
            container.removeView((View) object);
        }
    }
}
